# !/usr/bin bash

echo "=========当前部署为正式环境，请检查是否正确========="

echo "服务器信息："
echo "后台管理容器：zbq-mall-manager    端口：8090"
echo "API接口容器：zbq-mall-api        端口：8091"
echo "资源目录：/opt/volume"
echo "redis配置：/opt/volume/redis"
echo "nginx配置：/opt/volume/nginx"

v_time=`date "+%Y-%m-%d_%H-%M-%S"`

# 判断文件是否存在 不存在则退出
if [[ ! -f zbq-mall-manager/target/app.jar ]];then
  echo "=========没有找到部署文件，请先执行项目打包。==========="
  exit 1;
fi

# 判断文件是否存在 不存在则退出
if [[ ! -f zbq-mall-api/target/app.jar ]];then
  echo "=========没有找到部署文件，请先执行项目打包。==========="
  exit 1;
fi

echo "确定部署文件是否最新？部署环境是否正确？(y/n)"
read check
if [[  $check != 'y' ]]; then
  echo "部署终止。"
  exit 1
fi

echo "是否备份？(y/n)"
read isbak

echo "请输入您的服务器用户名:"
read username
if [[ ! $username ]]; then
  echo "$0 username is required"
  exit 1
fi

remotessh=${username}@121.41.15.25

#设置一下
stty -echo
echo "请输入您的服务器密码："
read password
stty echo
if [[ ! $password ]]; then
  echo "$0 password is required"
  exit 1
fi


if [[  $isbak == 'y' ]]; then
  sshpass -p "$password" ssh ${remotessh} "cd /home/deploy ; cp ./mall-manager/app.jar ./bak/${v_time}_manager.jar"
  echo "备份完成:/home/deploy/bak/${v_time}_manager.jar"

  sshpass -p "$password" ssh ${remotessh} "cd /home/deploy ; cp ./mall-api/app.jar ./bak/${v_time}_api.jar"
  echo "备份完成:/home/deploy/bak/${v_time}_api.jar"
fi

echo '开始传输管理后台安装包...'

sshpass -p "$password" scp zbq-mall-manager/target/app.jar ${remotessh}:/home/deploy/mall-manager/app.jar

echo '管理后台安装包传输完成。'

echo '开始传输 API接口 安装包...'
sshpass -p "$password" scp zbq-mall-api/target/app.jar ${remotessh}:/home/deploy/mall-api/app.jar

echo '所有安装包传输完成，准备就绪.'



